package client.presen;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import javax.swing.JPanel;

import client.comunicaciones.Proxy;
import client.presen.componentes.JPTablero;
import client.presen.componentes.JPTableroKenken;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class JFSudoku extends JFrame implements IVentanaJugador  {

	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JList jlSudokus = null;
	private JTabbedPane jTabbedPane = null;
	private JLabel jLabel = null;
	private JTextField jtfServidor = null;
	private JLabel jLabel1 = null;
	private JTextField jtfPuerto = null;
	private JButton jbConectar = null;
	private DefaultListModel modeloSudokus = new DefaultListModel();

	
	/**
	 * This is the default constructor
	 */
	public JFSudoku() {
		super();
		initialize();
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(569, 527);
		this.setContentPane(getJContentPane());
		this.setTitle("Sudoku");
		this.addWindowListener(new java.awt.event.WindowAdapter() { 
			public void windowClosing(java.awt.event.WindowEvent e) {    
				System.exit(0);
			}
		});
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel = new JLabel();
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(null);
			jContentPane.setVisible(true);			
			jLabel.setBounds(5, 12, 59, 25);
			jLabel.setText("Servidor:");
			jLabel1.setBounds(223, 15, 65, 23);
			jLabel1.setText("Puerto:");
			
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJTabbedPane(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJtfServidor(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJtfPuerto(), null);
			jContentPane.add(getJbConectar(), null);
			jContentPane.add(getJScrollPane1(), null);
			jContentPane.add(getJtfNombre(), null);
			jContentPane.add(getJbCrearNuevo(), null);
		}
		return jContentPane;
	}
	
	
	
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(5, 88, 126, 315);
			jScrollPane.setViewportView(getJlSudokus());
		}
		return jScrollPane;
	}

	private JList getJlSudokus() {
		if (jlSudokus == null) {
			jlSudokus = new JList();
		}
		jlSudokus.setModel(this.modeloSudokus);
		jlSudokus.addMouseListener(new java.awt.event.MouseAdapter() { 
			public void mouseClicked(java.awt.event.MouseEvent e) {  
				//if (e.getClickCount()==2)
					//showSudoku();
			}
		});
		return jlSudokus;
	}

	/*protected void showSudoku() {
		try {
			int index=this.jlSudokus.getSelectedIndex();
			if (index==-1) return;
			String id=this.jlSudokus.getSelectedValue().toString();
			String sudoku=this.proxy.getSudoku(id);
			JPTablero j=new JPTablero(sudoku, this);
			this.jTabbedPane.add(id, j);
			log(sudoku);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			logError(ex.toString());
		}
	}*/

	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setBounds(136, 57, 420, 348);
		}
		return jTabbedPane;
	}

	private JTextField getJtfServidor() {
		if (jtfServidor == null) {
			jtfServidor = new JTextField();
			jtfServidor.setBounds(64, 17, 148, 21);
			jtfServidor.setText("127.0.0.1");
		}
		return jtfServidor;
	}

	private JTextField getJtfPuerto() {
		if (jtfPuerto == null) {
			jtfPuerto = new JTextField();
			jtfPuerto.setBounds(288, 19, 86, 19);
			jtfPuerto.setText("2395");
		}
		return jtfPuerto;
	}

	private JButton getJbConectar() {
		if (jbConectar == null) {
			jbConectar = new JButton();
			jbConectar.setBounds(447, 14, 105, 22);
			jbConectar.setText("Conectar");
			jbConectar.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					conectar();
				}
			});
		}
		return jbConectar;
	}

	protected void conectar() {
		try {
			this.proxy=new Proxy(this.jtfNombre.getText(), this);
			log("Jugador escuchando");
		}
		catch (Exception ex) {
			logError(ex.toString());
		}
		try {
			this.proxy.conectar(this.jtfServidor.getText(), this.jtfPuerto.getText());
			log("Conectado");
			this.showSudokus();
		} catch (MalformedURLException e) {
			logError("URL mal formada");
		} catch (RemoteException e) {
			logError("Error de comunicaciones: " + e.toString()+"\nServidor activado de kenken. Si desea jugar al sudoku cambie el servidor");
		} catch (NotBoundException e) {
			logError("El servidor no está escuchando");
		}
	}


	private void showSudokus() {
		try {
			this.jlSudokus.removeAll();
		//	Vector<String> sudokus=this.proxy.getSudokus();
		//	for (int i=0; i<sudokus.size(); i++)
			//	modeloSudokus.addElement(sudokus.get(i));
		}
		catch (Exception ex) {
			logError(ex.toString());
		}
	}


	private void logError(String mensaje) {
		mensaje="<font color=red>" + mensaje + "</font>" + this.jepLog.getText();
		jepLog.setText(mensaje);
		jepLog.select(0, 0);
	}


	private void log(String mensaje) {
		mensaje="<font color=blue>" + mensaje + "</font>" + this.jepLog.getText();
		jepLog.setText(mensaje);
		jepLog.select(0, 0);
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */    
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(6, 407, 551, 87);
			jScrollPane1.setViewportView(getJepLog());
		}
		return jScrollPane1;
	}
	/**
	 * This method initializes jEditorPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */    
	private JEditorPane getJepLog() {
		if (jepLog == null) {
			jepLog = new JEditorPane();
			jepLog.setContentType("text/html");
		}
		return jepLog;
	}


	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJtfNombre() {
		if (jtfNombre == null) {
			jtfNombre = new JTextField();
			jtfNombre.setBounds(377, 20, 67, 17);
			jtfNombre.setText("maco");
		}
		return jtfNombre;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJbCrearNuevo() {
		if (jbCrearNuevo == null) {
			jbCrearNuevo = new JButton();
			jbCrearNuevo.setBounds(5, 57, 124, 24);
			jbCrearNuevo.setText("Crear nuevo");
			jbCrearNuevo.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (jbCrearNuevo.getText().equals("Guardar"))
						guardar();
					else
						crearNuevo();
				}
			});
		}
		return jbCrearNuevo;
	}
	/**
	 * 
	 */
	protected void guardar() {
		for (int i=0; i<this.jTabbedPane.getTabCount(); i++) {
			String title=this.jTabbedPane.getTitleAt(i);
			if (title.equals("Nuevo")) {
				JPTablero j=(JPTablero) this.jTabbedPane.getComponentAt(i);
				String sudoku=j.getSudoku();
				try {
				//	this.proxy.guardar(sudoku);
					this.jbCrearNuevo.setText("Crear nuevo");
					this.jTabbedPane.remove(i);
					log("Sudoku guardado. Vuelva a conectarse para verlo");
				} catch (Exception e) {
					logError("Error: " + e.toString());
				}
			}
		}
	}

	/**
	 * 
	 */
	protected void crearNuevo() {
		this.jbCrearNuevo.setText("Guardar");
		JPTablero j=new JPTablero(this);
		this.jTabbedPane.add(j, "Nuevo");
		
	}


	/**
	 *  
	 * @uml.property name="proxy"
	 * @uml.associationEnd inverse="jFSudoku:sudoku.client.comunicaciones.Proxy" multiplicity="(0 1)"
	 * 
	 */
	protected Proxy proxy;

	private JScrollPane jScrollPane1 = null;
	private JEditorPane jepLog = null;
	private JTextField jtfNombre = null;
	private JButton jbCrearNuevo = null;
	/**
	 *  
	 * @uml.property name="proxy"
	 * 
	 */
	public Proxy getProxy() {
		return proxy;
	}

	/**
	 *  
	 * @uml.property name="proxy"
	 * 
	 */
	public void setProxy(Proxy proxy) {
		this.proxy = proxy;
	}

	/* (non-Javadoc)
	 * @see sudoku.client.presen.IVentanaJugador#jugadorColoca(byte, int, int)
	 */
	public void jugadorColoca(byte idCuadricula, byte numero, byte fila, byte columna) throws RemoteException {
		int index=this.jTabbedPane.getSelectedIndex();
		String id=this.jTabbedPane.getTitleAt(index);
		if (id.equalsIgnoreCase("Nuevo")) return;
		//this.proxy.poner(id, idCuadricula, numero, fila, columna);
	}

	/* (non-Javadoc)
	 * @see sudoku.client.presen.IVentanaJugador#actualizaSudoku(int, java.lang.String)
	 */
	public void actualizaSudoku(int id, String sudoku) throws RemoteException {
		log("Recibo cambio sobre el " + id);
		for (int i=0; i<this.jTabbedPane.getTabCount(); i++) {
			String title=this.jTabbedPane.getTitleAt(i);
			if (title.equals(""+id)) {
				JPTablero j=(JPTablero) this.jTabbedPane.getComponentAt(i);
				j.load(sudoku);
			}
		}
	}

	/* (non-Javadoc)
	 * @see sudoku.client.presen.IVentanaJugador#jugadorQuita(byte, byte)
	 */
	public void jugadorQuita(byte idCuadricula, byte fila, byte columna) throws RemoteException {
		int index=this.jTabbedPane.getSelectedIndex();
		String id=this.jTabbedPane.getTitleAt(index);
		//this.proxy.quitar(id, idCuadricula, fila, columna);
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
